home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14955 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.4 KB

  1. Path: nntp.teleport.com!usenet
  2. From: GHouck <hksys@teleport.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Trouble Reading Binary Data from MicroVAX III
  5. Date: 16 Apr 1996 09:02:55 GMT
  6. Organization: systems hk
  7. Message-ID: <4kvnnv$o4o@nadine.teleport.com>
  8. References: <4kh8d8$kjc@newshound.csrv.uidaho.edu> <4ktn69INNohu@keats.ugrad.cs.ubc.ca>
  9. NNTP-Posting-Host: ip-pdx02-45.teleport.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.22 (Windows; I; 32bit)
  14.  
  15. c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku) wrote:
  16. >In article <4kh8d8$kjc@newshound.csrv.uidaho.edu>,
  17. >Ron Patterson <patt9451@uidaho.edu> wrote:
  18. > >Hello all,
  19. > >
  20. > >I am having trouble reading binary data that was created with a MicroVAX III
  21. > >workstation.  I am able to read int's, and char's o.k. but the majority of
  22. > >the data is a large number of floating point values.  fread() returns the 
  23. > >correct number of floating point numbers read but the data is a mess of zeros 
  24. > >and extremely large or small numbers.  Is this a big endian/little endian 
  25. > >problem (my program is being compiled and run on a PC with a 32 bit C compiler) 
  26. > >or bit ordering problem?  I know the size of a float on both systems is the
  27. > >same (4 bytes).  Any help would be great!
  28. >
  29. >The Vax doesn't use IEEE floating point representation, as far as I remember. I
  30. >don't have references off-hand that would tell me what that representation is,
  31. >but all I can say is: be prepared to do some bit twiddling to recover the
  32. >values.
  33. >
  34. >One of the textbooks by William Stallings, or just about any general text on
  35. >computer architectures, should have a description.
  36. >-- 
  37. >I'm not really a jerk, but I play one on Usenet.
  38.  
  39. Ron,
  40.  
  41. Here is a routine I found somewhere.  I take no credit or blame for its look
  42. or feel.  It does seem to work for my MicroStation CAD work (which exists on
  43. VAXen and Intels).  Yours to keep.
  44.  
  45. Good Luck,
  46. Geoff Houck
  47. hksys@teleport.com
  48.  
  49. /* ---------------------------------------------------------------------------
  50. vaxToIntel -    Converts a vax float_d stored as 8 bytes to an Intel double.
  51. --------------------------------------------------------------------------- */
  52. double vaxToIntel ( double *vaxFloat )
  53. {
  54.   unsigned char    vaxBytes[8];
  55.   long        lngBytes[8];
  56.   int        i, j, k;
  57.   int        res, temp, bits[64], sub, sign;
  58.   double    exp, frac, f1, result;
  59.  
  60.   memmove( vaxBytes,vaxFloat,8 );
  61.   for( i=0; i<8; i++ )
  62.     lngBytes[i] = vaxBytes[i];
  63.  
  64.   for( j=0; j<8; j++ ) {
  65.     switch( j ) {
  66.       case 0:
  67.         k = 1;
  68.         break;
  69.       case 1:
  70.         k = 0;
  71.         break;
  72.       case 2:
  73.         k = 3;
  74.         break;
  75.       case 3:
  76.         k = 2;
  77.         break;
  78.       case 4:
  79.         k = 5;
  80.         break;
  81.       case 5:
  82.         k = 4;
  83.         break;
  84.       case 6:
  85.         k = 7;
  86.         break;
  87.       case 7:
  88.         k = 6;
  89.         break;
  90.     }
  91.  
  92.     temp = lngBytes[k];
  93.     sub = 128;
  94.  
  95.     for( i=(j*8); i<(j*8)+7; ++i ) {
  96.       res = temp / sub;
  97.       bits[i] = res;
  98.       if( res == 1 ) {
  99.         temp = temp - sub;
  100.         sub = sub / 2;
  101.       }
  102.       bits[(j*8)+7] = temp;
  103.     }
  104.   }
  105.  
  106.   sign = bits[0];
  107.   exp = 0;
  108.   res = 0;
  109.   sub = 128;
  110.  
  111.   for( i=1; i<9; i++ ) {
  112.     res = res + ( bits[i] * sub );
  113.     sub = sub/2;
  114.   }
  115.  
  116.   res = res - 128;
  117.   exp = pow( 2,res );
  118.   frac = .5;
  119.   f1 = .25;
  120.  
  121.   for( i=9; i<65; i++ ) {
  122.     frac = frac + ( bits[i] * f1 );
  123.     f1 = f1/2;
  124.   }
  125.  
  126.   result = frac * exp;
  127.   if( sign == 1 ) {
  128.     result = 0 - result;
  129.   }
  130.   return ( result );
  131. }
  132.  
  133.  
  134.